<?php
namespace addons\yalimedia\service;

use app\admin\model\yalimedia\course\Contentlog;
use app\common\services\UrlService;

class Teacher
{

    public $model;
    public $error;
    public function __construct()
    {
        $this->model = new \app\admin\model\yalimedia\Teacher();
    }

    /**
     * 获取教师列表
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getTeacherList($where=[],$page,$limit){
        $count = $this->model->where($where)->count();
        if($page){
            $data = $this->model->where($where)->page($page,$limit)->order("weigh asc")->select();
        }else{
            $data = $this->model->where($where)->order("weigh asc")->select();
        }

        return ['total'=>$count,'data'=>$data];
    }


    /**
     * 获取教师详情
     * @param $page
     * @param $limit
     * @throws \think\Exception
     */
    public function getTeacherInfo($where=[]){

        $info = $this->model->where($where)->find();
        if(empty($info)){
            $this->error = "当前教师不存在";
        }
        // $info['image'] = request()->domain()."/".$info['image'];
        $info['image'] = urlService::getFileUrl($info['image']);
        return $info;
    }

    /**
     * 获取教师课程节数
     */
    public function getCourseNum($tid){

        $courseModel = new \app\admin\model\yalimedia\Course();
        $num = $courseModel
            ->alias("course")
            ->join("yalimedia_course_content content","course.id=content.course_id")
            ->where(['course.teacher_id'=>$tid])
            ->count();

        return $num;
    }

    /**
     * 获取学习人数
     */
    public function getStudyNum($tid){

        $contentLogModel = new Contentlog();
        $num = $courseInfo['study_count'] = $contentLogModel
            ->alias("log")
            ->join("yalimedia_course course","log.course_id=course.id")
            ->where(['course.teacher_id'=>$tid,'course.status'=>1])
            ->group("log.user_id")
            ->count();

        return $num;
    }

}